import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.lang.*;

public class URLProvider {
	private boolean stopped=true;
	
	public URLProvider(List<String> urls, int portNumber)
	{
		
	
		
		try {
			ServerSocket serverSocket = new ServerSocket(portNumber);
			//Thread.sleep(3000);
			    System.out.println("Before accept()");
				
				
				Socket clientSocket = serverSocket.accept();
				
				TCPout outthread= new TCPout(clientSocket, urls);
				
				TCPin inthread= new TCPin(clientSocket, urls);
				Thread ot = new Thread(outthread);
				Thread it = new Thread(inthread);

				ot.start();
				it.start();
				
				ot.join();
				it.join();
				
		} catch (IOException | InterruptedException e) {
			System.out
					.println("Exception caught when trying to listen on port "
							+ portNumber + " or listening for a connection");
			System.out.println(e.getMessage());
		
		}
	}
	public static void main(String[] args) throws IOException,
			InterruptedException {
		int portNumber = 31038;
		
		ArrayList<String> urls = new ArrayList<String>();
		urls.add("www.baidu.com");
		urls.add("www.google.com");
		urls.add("www.nus.edu.sg");
		urls.add("www.bbc.com");
		urls.add("www.w3schools.com");
		urls.add("www.lib.nus.edu.sg");
		urls.add("meta.stackoverflow.com");
		urls.add("careers.stackoverflow.com");
		urls.add("stackexchange.com");
		urls.add("www.heartinternet.co.uk");
		urls.add("www.eukhost.com");
		urls.add("bbs.english.sina.com");
		urls.add("sina.echineselearning.com");
		urls.add("roll.news.sina.com.cn");
		urls.add("www.wix.com");
		urls.add("www.weibo.com");
		urls.add("cn.nus.edu.sg");
		urls.add("president.nus.edu.sg");
		urls.add("www.overseas.nus.edu.sg");
		urls.add("www.fas.nus.edu.sg");
		urls.add("www.nusentrepreneurshipcentre.sg");
		urls.add("www.echineselearning.com");
		urls.add("www.nusri.cn");
		urls.add("security.stackexchange.com");
		urls.add("chi.nus.edu.sg");
		urls.add("resources.echinesellcademia.stackexchange.com");
		urls.add("earning.com");
		urls.add("www.iss.nus.edu.sg");
		urls.add("matheducators.stackexchange.com");
		urls.add("codereview.stackexchange.com");
		urls.add("executive-education.nus.edu");
		urls.add("tridion.stackexchange.com");
		urls.add("www.rmi.nus.edu.sg");
		urls.add("www.stmi.nus.edu.sg");
		urls.add("aviation.stackexchange.com");
		urls.add("www.cdtl.nus.edu.sg");
		urls.add("www.chinaview.cn");
		urls.add("www.cit.nus.edu.sg");
		urls.add("workplace.stackexchange.com");
		urls.add("salesforce.stackexchange.com");
		urls.add("utown.nus.edu.sg");
		urls.add("www.chinadaily.com.cn");
		urls.add("physics.stackexchange.com");
		urls.add("gis.stackexchange.com");
		urls.add("english.caixin.com");
		urls.add("ux.stackexchange.com");
		urls.add("www.shanghaidaily.com");
		urls.add("raspberrypi.stackexchange.com");
		urls.add("askubuntu.com");
		urls.add("photo.stackexchange.com");
		urls.add("www.ecns.cn");
		urls.add("tex.stackexchange.com");
		urls.add("chess.stackexchange.com");
		urls.add("www.timeoutbeijing.com");
		urls.add("unix.stackexchange.com");
		urls.add("electronics.stackexchange.com");
		urls.add("www.globaltimes.cn");
		urls.add("campuslife.nus.edu.sg");
		urls.add("corp.sina.com.cn");
		urls.add("news.nus.edu.sg");
		urls.add("data.stackexchange.com");
		urls.add("mediakit.sina.com");
		urls.add("alumnet.nus.edu.sg");
		urls.add("serverfault.com");
		urls.add("www.yale-nus.edu.sg");
		urls.add("superuser.com");
		urls.add("webapps.stackexchange.com");
		urls.add("webmasters.stackexchange.com");
		urls.add("giving.nus.edu.sg");
		urls.add("gamedev.stackexchange.com");
		urls.add("www.youtube.com");
		urls.add("programmers.stackexchange.com");
		urls.add("nus.edu.sg");
		urls.add("apple.stackexchange.com");
		urls.add("m.nus.edu.sg");
		urls.add("sports.sina.com.cn");
		urls.add("auto.sina.com.cn");

		new URLProvider(urls, portNumber);
	//	boolean stopped=false;
      //  int i=0;// i is the index of url
		
					
					/*DataOutputStream out = new DataOutputStream(
							clientSocket.getOutputStream());
					BufferedReader in = new BufferedReader(new InputStreamReader(
							clientSocket.getInputStream()));
					*/
					//System.out.println("haha");
					//String tmp="www.baidu.com";
					
					/*int command,length;
					if((command=in.read())!=-1)
					{
						
						System.out.println("receive command"+command );
						if(command==10)
						{
							//length=in.read();
							System.out.println("The client needs 2 number of addresses");
							for(int k=0;k<2;k++)
							{
							if(i>=urls.size())
							{
								 out.writeBytes("\n");
								 System.out.println("url: \"\n sent");
							}
							else if (stopped)
							{
								System.out.println("The GUI has stopped");
							}
									else
									{
								out.writeBytes(urls.get(i) + "\n"+ "2000"+"\n");
								
								System.out.println("url: " + urls.get(i) + " sent");
								i++;
									}
								
								
							}
						}
						else if (command==0)
						{
						length=in.read();
						System.out.println("The URL list length is: "+ length);
						
						for(int k=0;k<length;k++)
						{
						String url=in.readLine();
							System.out.println(url);
						}
						stopped=false;
						
						}
						else if(command==1)
						{
							System.out.println("Resume command received");
							stopped=false;
						}
			
						else if (command==2)
						{
							System.out.println("Stop command received");
							stopped=true;
						}
						else if(command==3)
						{
							System.out.println("Save command received");
							System.out.println(in.read());
							String filename=in.readLine();
							System.out.println("File name is: "+filename);
						}
						
					}
					out.close();
					clientSocket.close();*/
					
					
					
				/*	System.out.println("hee");
					Thread.sleep(10000);
					
					System.out.println("After accept()");
					for(int k=0;k<urls.size();k++)
					{
				
					 Thread.sleep(5000);
				out.writeBytes(urls.get(i) + "\n"+ "2000"+"\n");
				System.out.println("url: " + urls.get(i) + " sent");
				i++;
					}
					while(true)
						
					{
						Thread.sleep(1000);
						 out.writeBytes("\n");
						 System.out.println("url: of \n sent");
					}
					
				
				*/
					

		
		
		
	}

	
	class TCPout implements Runnable{
	private	Socket clientSocket ;
	private List<String> urls=null;
	
	public TCPout(Socket s, List<String> l)
	{
		urls=l;
		this.clientSocket=s;
	}
	
	public void run()
	{
		
		try {
			DataOutputStream out = new DataOutputStream(
					clientSocket.getOutputStream());
		
		 int i=0;
		 
	   while (true)
	   {
		   Thread.sleep(3000);
		  
			
			if(i>=urls.size())
			{
				 out.writeBytes("\n");
				 System.out.println("url: \"\n sent");
			}
			else if (stopped)
			{
				System.out.println("The Webside wants stopped");
			}
					else
					{
						 System.out.println("The client needs 1 number of addresses");
						
				out.writeBytes(urls.get(i) + "\n"+ "2000"+"\n");
				
				System.out.println("url: " + urls.get(i) + " sent");
				i++;
					}
			
	   }
		} catch (IOException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	}
	
	
	
	class TCPin implements Runnable{
		
		private	Socket clientSocket ;
		private List<String> urls=null;
		
		public TCPin(Socket s, List<String> l)
		{
			urls=l;
			this.clientSocket=s;
		}
		
		public void run()
		{
			int i=0;
			BufferedReader in;
			try {
				in = new BufferedReader(new InputStreamReader(
						clientSocket.getInputStream()));
		
			int command,length;
			while(true)
			{
			if((command=in.read())!=-1)
			{
				
				System.out.println("receive command"+command );
				if(command==10)
				{
					//length=in.read(); do nothing
					
				}
				else if (command==0)
				{
					
					System.out.println("Observe that command received is 0");
				length=in.read();
				System.out.println("The URL list length is: "+ length);
				
				for(int k=0;k<length;k++)
				{
					System.out.print("base url read in: ");
					char tmp[]= new char [100];
				char c;
				int j=0;
					while((c=(char) in.read())!=-1)
					{
						if(c=='\0')
							break;
						tmp[j++]=c;
					}
					
				String url=new String(tmp);
					System.out.println(url);
				}
				stopped=false;
				
				}
				else if(command==1)
				{
					System.out.println("Resume command received");
					stopped=false;
				}
	
				else if (command==2)
				{
					System.out.println("Stop command received");
					stopped=true;
				}
				else if(command==3)
				{
					System.out.println("Save command received");
					System.out.println(in.read());
					String filename=in.readLine();
					System.out.println("File name is: "+filename);
				}
				
			}
			
			}
			
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		}
		
	}
}
